package com.ln.de1109.demo01;

import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import net.sf.cglib.proxy.Enhancer;
import java.lang.reflect.Method;

/**
 * @author Ln
 * @version 终极版
 * @date 2022/11/9 16:09
 */
public class ProxyFactory implements MethodInterceptor{
    //被代理的对象
    private Object target;

    public ProxyFactory(Object target) {
        this.target = target;
    }

    //获取代理对象
    public Object getInstance(){
        //创建一个工具---生成代理对象
        Enhancer enhancer = new Enhancer();
        //指定被代理的的父类
        enhancer.setSuperclass(target.getClass());
        //代理的回调方法
        enhancer.setCallback(this);
        return enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        checkout();
        Object invoke = method.invoke(target, objects);
        log();
        return invoke;
    }

    public void checkout(){
        System.out.println("=======校验内容");
    }

    public void log(){
        System.out.println("=======日志内容");
    }
}
